package com.teng.mrliuproject.ui.activity

import android.Manifest
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.textfield.TextInputLayout
import com.teng.mrliuproject.R
import com.teng.mrliuproject.base.RouteManages
import com.teng.mrliuproject.databinding.ActivityLoginBinding
import com.teng.mrliuproject.ui.vmodel.LoginVModel
import com.teng.mylibrary.base.BaseActivity

@Route(path = RouteManages.LOGIN_ACTIVITY)
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginVModel>() {

    override fun isTranslucentSutatus(): Boolean {
        return true
    }

    override fun initViewModel(): LoginVModel {
        return ViewModelProvider(this).get(LoginVModel::class.java)
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_login
    }


    override fun initView() {
        supportActionBar?.hide()
        binding.viewmodel = viewModel

        viewModel.myUc.checkLogin.observe(this, {
//            if (validateAccount(binding.tietAccount.text.toString()) && validatePassword(binding.tietPassword.text.toString())
//            ) {
            viewModel.canLogin.set(true)
            viewModel.loginApp()
//            }
        })

    }

    override fun initData() {
        rxPermissions.request(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
        )
            .subscribe {
                if (!it) {
                    finish()
                }
            }
    }


    /**
     * 验证用户名
     * @param account String
     * @return Boolean
     */
    private fun validateAccount(account: String): Boolean {
        if (account.isEmpty()) {
            showError(binding.tilAccount, "请输入用户名")
            return false
        }
        binding.tilAccount.isErrorEnabled = false
        return true
    }

    private fun validatePassword(password: String): Boolean {
        if (password.isEmpty()) {
            showError(binding.tilPassword, "请输入密码")
            return false
        }
        binding.tilPassword.isErrorEnabled = false
        return true
    }


    /**
     * TextInputLayout显示错误提示，并获取焦点123123
     * @param textInputLayout
     * @param error
     */
    private fun showError(textInputLayout: TextInputLayout, error: String) {
        textInputLayout.error = error
        textInputLayout.editText!!.isFocusable = true
        textInputLayout.editText!!.isFocusableInTouchMode = true
        textInputLayout.editText!!.requestFocus()
    }

}